package com.begye.smartloan.apptest.certify.adapter

import android.annotation.SuppressLint
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.recyclerview.widget.RecyclerView
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.ext.dpToPx
import com.begye.smartloan.apptest.ext.screenWidth
import org.json.JSONObject

class CertifySalaryImageAdapter(var dataList: List<JSONObject>, val onClickAction: (Int, Int) -> Unit) :
    RecyclerView.Adapter<CertifySalaryImageAdapter.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item_certify_salary_image, parent, false)

        val screenWidth = parent.context.screenWidth()
        val edgeWidth = parent.context.dpToPx(56F).toInt()
        val itemWidth = (screenWidth - edgeWidth) / 3
        val layoutParams = view.layoutParams
        layoutParams.width = itemWidth
        layoutParams.height = itemWidth
        view.layoutParams = layoutParams

        return ViewHolder(view)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val listItemCertifySalaryImageContent: RelativeLayout =
            view.findViewById(R.id.listItemCertifySalaryImageContent)

        private val listItemCertifySalaryImage: ImageView =
            view.findViewById(R.id.listItemCertifySalaryImage)

        private val listItemCertifySalaryDelete: ImageView =
            view.findViewById(R.id.listItemCertifySalaryDelete)

        private val listItemCertifySalaryAddContent: LinearLayout =
            view.findViewById(R.id.listItemCertifySalaryAddContent)


        @SuppressLint("SetTextI18n")
        fun bindData(position: Int, item: JSONObject) {

            val isAdd = item.optString("isAdd", "NO")
            val filePath = item.optString("path", "")

            if (filePath.isNotBlank()) {
                try {
                    val options = BitmapFactory.Options().apply {
                        inJustDecodeBounds = false
                    }
                    val bitmap = BitmapFactory.decodeFile(filePath, options)
                    listItemCertifySalaryImage.setImageBitmap(bitmap)

                } catch (e: Exception) {
                    //
                }
            }

            if (isAdd == "YES") {
                listItemCertifySalaryImageContent.visibility = View.GONE
                listItemCertifySalaryAddContent.visibility = View.VISIBLE

            } else {
                listItemCertifySalaryImageContent.visibility = View.VISIBLE
                listItemCertifySalaryAddContent.visibility = View.GONE
            }

            listItemCertifySalaryDelete.setOnClickListener {
                onClickAction.invoke(1, position)
            }

            listItemCertifySalaryAddContent.setOnClickListener {
                onClickAction.invoke(0, position)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(position, dataList[position])
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}